package com.example.buyticketlishi.adapter;

import android.content.Context;
import android.content.Intent;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.baselibrary.Utils.TimeUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.RetrufitHelp.BaseSubscriber;
import com.example.buyticketlishi.RetrufitHelp.ExceptionHandle;
import com.example.buyticketlishi.RetrufitHelp.RetrofitClient;
import com.example.buyticketlishi.bean.JJHSealTicketForTitleResultBean;
import com.example.buyticketlishi.bean.JJHhandSetRequestBean;
import com.example.buyticketlishi.bean.RzfindTimeControlListRequestBean;
import com.example.buyticketlishi.bean.RzfindTimeControlListResultBean;
import com.example.buyticketlishi.wightview.NumberSubAddView;
import com.example.buyticketlishi.wightview.TimeSelectDialogShow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SealAdapter extends RecyclerView.Adapter<SealAdapter.ViewHolder> {

    private Context context;
    private List<JJHSealTicketForTitleResultBean.DataBean> data;
    private RzfindTimeControlListRequestBean rzfindTimeControlListRequestBean;
    private String baseUrl;
    private Map<String, String> heads;
    private List<RzfindTimeControlListResultBean.DataBean> timeSelectList;   // 时段选择数据

    // 显示选择信息的对话框
    private TimeSelectDialogShow timeSelectDialogShow;

    private DataSelectListener timeSelectListener;

    private String bookTimeStr;
    private int posSelect = -1;
    private List<String> timeSelect;
    // 用 Map  来确保 数据单一

    public SealAdapter(Context context, List<JJHSealTicketForTitleResultBean.DataBean> data, String BaseUrl, Map<String, String> heads) {
        this.context = context;
        this.data = data;
        this.baseUrl = BaseUrl;
        this.heads = heads;

    }

    @Override
    public SealAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (rzfindTimeControlListRequestBean == null) {
            rzfindTimeControlListRequestBean = new RzfindTimeControlListRequestBean();
        }

        if (timeSelectList == null) {
            timeSelectList = new ArrayList<>();
        }

        View view = LayoutInflater.from(context).inflate(R.layout.seal_recycle, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SealAdapter.ViewHolder holder, final int position) {

        holder.listTitle.setText(data.get(position).getScenicTicketName());

        holder.listprice.setText("¥" + data.get(position).getPrice());
        //实名制标记   certificateFlag
        if (data.get(position).getCertificateFlag() == 1) {
            //1 是 存在实名制
            holder.haveName.setVisibility(View.VISIBLE);
        } else if (data.get(position).getCertificateFlag() == 2) {
            holder.haveName.setVisibility(View.INVISIBLE);
        }

        // 时段是否显示
        if (data.get(position).getBookByTimeFlag().equals("Y")) {
            //显示时段
            holder.bookTimeTip.setVisibility(View.VISIBLE);
            holder.bookTimeTitle.setVisibility(View.VISIBLE);
            holder.bookTime.setVisibility(View.VISIBLE);
            holder.btSelectTime.setVisibility(View.VISIBLE);
        } else {
            // 不显示时段
            holder.bookTimeTip.setVisibility(View.INVISIBLE);
            holder.bookTimeTitle.setVisibility(View.INVISIBLE);
            holder.bookTime.setVisibility(View.INVISIBLE);
            holder.btSelectTime.setVisibility(View.INVISIBLE);
        }

        if (data.get(position).getSaleSum() == null) {
            holder.ticketNumber.setAmountValue(0);
        } else {
            holder.ticketNumber.setAmountValue(Integer.parseInt(data.get(position).getSaleSum()));
        }
        //根据选着的人数进行数据填充
        holder.ticketNumber.setOnAmountChangeListener(new NumberSubAddView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                //数据变化了，那么就要对数据进行赋值
                data.get(position).setSaleSum(amount + "");
                // 这里是为没有时段添加数据

                JJHhandSetRequestBean.ProductParamListBean productParamListBean = new JJHhandSetRequestBean.ProductParamListBean();

                data.get(position).setArriveDT(TimeUtil.getLocationTimeNew(TimeUtil.FORMAT_DATE_EN));
                data.get(position).setScenicTicketId(data.get(position).getScenicTicketId());
                data.get(position).setShiftsId("");
                data.get(position).setSaleSum(holder.ticketNumber.getValue());
                data.get(position).setCustomerDiscountType("1");
                // 没有时段就没有时段数据
//                for (int i = 0; i < timeSelect.size(); i++) {
//                    JJHhandSetRequestBean.ProductParamListBean.TimeControlParamListBean timeControlParamListBean = new JJHhandSetRequestBean.ProductParamListBean.TimeControlParamListBean();
//                    timeControlParamListBean.setProjectId(timeSelectList.get(posSelect).getProjectId());
//                    timeControlParamListBean.setOrdTimeControlId(timeSelectList.get(posSelect).getTimeControlList().get(i).getTimeControlId());
//                }

//                dataMap.put(position + "", productParamListBean);
                timeSelectListener.dataSelect(data);
            }
        });

        holder.btSelectTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //选择时段的时候，先选择游玩人数
                if (Integer.parseInt(holder.ticketNumber.getValue()) <= 0) {
                    //如果小于或者等于0 那么就提示先选着游玩人数
                    SelfToastUtil.getInstance().show("请先选着游玩人数，在选择时段信息");
                    return;
                }

                //选择时段的点击事件， 获取 scenicTicketId  然后去请求时段信息
                getTimeSelect(position, data.get(position).getScenicTicketId() + "", Integer.parseInt(holder.ticketNumber.getValue()));
            }
        });

        // 这个地方去刷新控件   // 这里是为 有时段的添加数据，没有时段是不会走这里的
        if (posSelect != -1 && posSelect == position) {

            //如果条目的人数为0 ，那么就不去 赋值
            String saleSum = data.get(position).getSaleSum();
            if (saleSum.equals("0")) {
                return;
            }

//            holder.bookTime.setText(bookTimeStr);
            JJHhandSetRequestBean.ProductParamListBean productParamListBean = new JJHhandSetRequestBean.ProductParamListBean();

            data.get(position).setArriveDT(TimeUtil.getLocationTimeNew(TimeUtil.FORMAT_DATE_EN));
            data.get(position).setScenicTicketId(data.get(position).getScenicTicketId());
            data.get(position).setShiftsId("");
            data.get(position).setSaleSum(holder.ticketNumber.getValue());
            data.get(position).setCustomerDiscountType("1");
            data.get(position).setTimeShow(bookTimeStr);
            List<JJHSealTicketForTitleResultBean.DataBean.timeControlParamList> timeList = new ArrayList<>();
            int minNumber = 0;
            for (int i = 0; i < timeSelect.size(); i++) {
                JJHSealTicketForTitleResultBean.DataBean.timeControlParamList timeControlParamListBean = new JJHSealTicketForTitleResultBean.DataBean.timeControlParamList();

                String projectId = timeSelectList.get(posSelect - 1).getProjectId() + "";
                timeControlParamListBean.setProjectId(projectId);

                String ordTimeControlId = timeSelectList.get(posSelect - 1).getTimeControlList().get(Integer.parseInt(timeSelect.get(i))).getTimeControlId() + "";
                timeControlParamListBean.setOrdTimeControlId(ordTimeControlId);

                if (minNumber == 0) {
                    minNumber = timeSelectList.get(posSelect - 1).getTimeControlList().get(Integer.parseInt(timeSelect.get(i))).getResidueSum();
                }else {
                    if (minNumber >= timeSelectList.get(posSelect - 1).getTimeControlList().get(Integer.parseInt(timeSelect.get(i))).getResidueSum()) {
                        minNumber = timeSelectList.get(posSelect - 1).getTimeControlList().get(Integer.parseInt(timeSelect.get(i))).getResidueSum();
                    }
                }

                timeList.add(timeControlParamListBean);
            }
            data.get(position).setMinResidueSum(minNumber);
            data.get(position).setTimeData(timeList);
//            dataMap.put(posSelect - 1 + "", productParamListBean);
            timeSelectListener.dataSelect(data);
        }

        String saleSum = data.get(position).getSaleSum();
        if (saleSum.equals("0")) {
            holder.bookTime.setText("");
            return;
        } else {
            holder.bookTime.setText(data.get(position).getTimeShow());
        }

    }

    private void getTimeSelect(final int pos, String s, final int peopleNumber) {
        rzfindTimeControlListRequestBean.setScenicTicketId(s);
        rzfindTimeControlListRequestBean.setSaleChannelType("12");
        rzfindTimeControlListRequestBean.setControlDate(TimeUtil.getLocationTimeNew(TimeUtil.FORMAT_DATE_EN));
        RetrofitClient.getInstance(context, baseUrl, heads).RzfindTimeControlListByScenicTicket(rzfindTimeControlListRequestBean, new BaseSubscriber<RzfindTimeControlListResultBean>(context) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);
            }

            @Override
            public void onNext(RzfindTimeControlListResultBean rzfindTimeControlListResultBean) {
                if (rzfindTimeControlListResultBean.getCode().equals("200")) {

                    if (rzfindTimeControlListResultBean.getData().get(0) != null && rzfindTimeControlListResultBean.getData().get(0).getTimeControlList().size() >= 0) {
                        timeSelectList.clear();
                        timeSelectList.addAll(rzfindTimeControlListResultBean.getData());
                        timeSelectList.addAll(rzfindTimeControlListResultBean.getData());
                        timeSelectList.addAll(rzfindTimeControlListResultBean.getData());
                        //要做数据填充，不然数据不好做
                        for (int i = 0; i < timeSelectList.size(); i++) {
                            timeSelectList.get(i).setSelectClick(false);
                            for (int i1 = 0; i1 < timeSelectList.get(i).getTimeControlList().size(); i1++) {
                                timeSelectList.get(i).getTimeControlList().get(i1).setClick(false);
                                timeSelectList.get(i).getTimeControlList().get(i1).setClickPos("-1");
                            }
                        }
                        //获取到数据，那么就去显示
                        showTimeSelect(pos, timeSelectList, peopleNumber);
                    } else {
                        SelfToastUtil.getInstance().show("该票无时段信息");
                    }

                } else {
                    SelfToastUtil.getInstance().show(rzfindTimeControlListResultBean.getMessage());
                }
            }
        });
    }

    private void showTimeSelect(int pos, List<RzfindTimeControlListResultBean.DataBean> timeSelectList, int peopleNumber) {
        // 显示时段信息，并让用户选择
        timeSelectDialogShow = new TimeSelectDialogShow(pos, context, timeSelectList, peopleNumber);
        timeSelectDialogShow.show();
        timeSelectDialogShow.SetTimeSelectAllListener(new TimeSelectDialogShow.TimeSelectAllListener() {
            @Override
            public void tiemSelectResult(int pos, String timeSelectString, List<String> stringList) {
                timeSelect = stringList;
                bookTimeStr = timeSelectString;
                posSelect = pos;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView listTitle;
        private final TextView listprice;
        private final TextView haveName;

        private final TextView bookTimeTip;
        private final TextView bookTimeTitle;
        private final TextView bookTime;

        private final TextView btSelectTime;
        private final NumberSubAddView ticketNumber;
        private final TextView tvBuyKnow;


        public ViewHolder(View itemView) {
            super(itemView);
            listTitle = itemView.findViewById(R.id.tv_seal_title_name);
            listprice = itemView.findViewById(R.id.tv_seal_price);
            haveName = itemView.findViewById(R.id.tv_seal_have_name);
            tvBuyKnow = itemView.findViewById(R.id.tv_seal_price_know);

            // 时段信息
            bookTimeTip = itemView.findViewById(R.id.tv_seal_selete_time_tip);
            bookTimeTitle = itemView.findViewById(R.id.tv_seal_selete_time_title);
            bookTime = itemView.findViewById(R.id.tv_seal_selete_time);
            btSelectTime = itemView.findViewById(R.id.bt_seal_selete_time_tip);

            ticketNumber = itemView.findViewById(R.id.nsa_ticket_number);

        }
    }

    public interface DataSelectListener {
        void dataSelect(List<JJHSealTicketForTitleResultBean.DataBean> data);
    }

    public void setDataSelectListener(DataSelectListener timeSelectListener) {
        this.timeSelectListener = timeSelectListener;
    }
}
